# ============================================================
# by howlink *
# Filename       : addTnsname.sh
#
# Description:  create tnsname entres  
# exmple: ./addTnsname.sh orcl 192.168.2.228 192.168.2.229
# =============================================================
#! /bin/sh
set +x
if [ $# -lt 3 ]; then
echo 'parameters is less 3'
   exit 1
fi
sid=$1
sip=$2
tip=$3

orahome=`cat /opt/howlink/lb-agent/application.properties | grep "^agent.oracle-home" | awk -F'=' '{ print $2 }' | sed 's/ //g'`
if [ -z "$orahome" ];then
    orahome=`grep -wi "$sid:" $otab | grep "^$sid" | head -n1 | cut -f2 -d":"`
fi

tnsname=$orahome/network/admin/tnsnames.ora
`cat $tnsname |grep hlprimary >/dev/null`
if [ $? -eq 0 ]; then
    echo 'source tnsname is already in tnsname.ora'
else

echo "
hlprimary =
  (DESCRIPTION =
    (ADDRESS = 
      (PROTOCOL = TCP)
      (HOST = $sip)
      (PORT = 1521)) 
    (CONNECT_DATA = 
      (SERVER = DEDICATED) 
      (SERVICE_NAME = $sid)
    )  
  ) " >> $tnsname
fi

`cat $tnsname |grep hlstandby1 >/dev/null`
if [ $? -eq 0 ]; then
echo 'target tnsname is already in tnsname.ora'
else
echo "
hlstandby1 = 
  (DESCRIPTION =  
    (ADDRESS = 
      (PROTOCOL = TCP)
      (HOST = $tip)
      (PORT = 1521)) 
    (CONNECT_DATA = 
      (SERVER = DEDICATED) 
      (SERVICE_NAME = $sid) 
    ) 
  ) " >> $tnsname
fi
